package commands

import (
	"fmt"
	"os"
)

var cmds = map[string]*Command{}

func AddCommand(cmd *Command) {
	cmds[cmd.Cmd] = cmd
}

func Main(appName string) {
	args := os.Args
	if len(args) <= 1 {
		// 没有参数
		printUsages(appName)
		return
	}
	command := args[1]

	cmd, ok := cmds[command]
	if !ok {
		fmt.Printf("Error: command [%s] is not find\n", command)
		printUsages(appName)
		return
	}

	cmd.EnterPoint(appName, args)
}

func printUsages(appName string) {
	fmt.Println("Usage:")
	fmt.Printf("    %s [command]\n", appName)
	fmt.Println()
	fmt.Println("Available Commands:")
	for _, c := range cmds {
		fmt.Printf("    %-10s %s\n", c.Cmd, c.Describtion)
	}
}
